/*
* @file_name: task_executor-inl.h
* @date: 2021/08/30
* @author: oaho
* Copyright @ hz oaho, All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef _OAHO_TASK_EXECUTOR_INL_H
#define _OAHO_TASK_EXECUTOR_INL_H

#include "task_executor.h"


template<typename _policy_>
inline void task_executor<_policy_>::start()
{
	_execute_.start();
}
template<typename _policy_>
inline void task_executor<_policy_>::stop()
{
	_execute_.stop();
}

template<typename _policy_>
template<typename Func, typename...Args> 
void task_executor<_policy_>::async(Func&& f, Args&&...args)
{
	auto func = std::move(std::bind(f, std::forward<Args>(args)...));
	_execute_.put(std::ref(func));
}

template<typename mutex_t, typename alloc_t>
async_executor<mutex_t, alloc_t>::async_executor():quit(false), _th_executor(nullptr){}

template<typename mutex_t, typename alloc_t>
async_executor<mutex_t, alloc_t>::~async_executor()
{
	stop();
	_th_executor = nullptr;
}

template<typename mutex_t, typename alloc_t>
inline void async_executor<mutex_t, alloc_t>::start()
{
	if (!_th_executor)
	{
		_th_executor.reset(new std::thread([&]{
			this->run();
		}));
		quit = false;
	}
}
template<typename mutex_t, typename alloc_t>
void async_executor<mutex_t, alloc_t>::run()
{
	/*
		memory barrier, when quit, task que may have task not to execute
	*/
	while (!quit)
	{
		decltype(task_que) _tmp_task_que_;
		{
			mutex_t _m(mtx);
			_tmp_task_que_.swap(task_que);
		}
		while (!_tmp_task_que_.empty())
		{
			auto __task = std::move(_tmp_task_que_.front());
			if (__task)__task();
			_tmp_task_que_.pop_front();
		}
		std::this_thread::sleep_for(std::chrono::microseconds(200));
	}
}

template<typename mutex_t, typename alloc_t>
inline void async_executor<mutex_t, alloc_t>::stop()
{
	quit = true;
	if (_th_executor && _th_executor->joinable())_th_executor->join();
}

template<typename mutex_t, typename alloc_t>
template<typename Func>
inline void async_executor<mutex_t, alloc_t>::put(Func&& f)
{
	mutex_t _t(mtx);
	task_que.emplace_back(std::move(f));
}


using async_executor_mt_ = task_executor<async_executor<std::lock_guard<std::mutex>>>;
using sync_executor_st_ = task_executor<sync_executor>;
#endif // ! _OAHO_TASK_EXECUTOR_INL_H



